package com.wannengbang.cloudleader.homepage;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ethanhua.skeleton.RecyclerViewSkeletonScreen;
import com.ethanhua.skeleton.Skeleton;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.IncomeStatisticsBean;
import com.wannengbang.cloudleader.bean.RevenueListBean;
import com.wannengbang.cloudleader.bean.UserInfoBean;
import com.wannengbang.cloudleader.homepage.adapter.RevenueOverviewListAdapter;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.homepage.model.IHomePageModel;
import com.wannengbang.cloudleader.utils.DateTimeUtil;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.utils.SPManager;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.ViewLoading;
import com.wannengbang.cloudleader.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class IncomeStatisticsActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_order_fee_count_total)
    TextView tvOrderFeeCountTotal;
    @BindView(R.id.tv_look_details)
    TextView tvLookDetails;


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private IHomePageModel homePageModel;
    private UserInfoBean userInfoBean;
    private BottomSheetDialog selectPicDialog;
    private int maker;
    private int hb_operation_type;
    private int agency;

    public List<RevenueListBean.DataBean> dataBeans = new ArrayList<>();
    private RevenueOverviewListAdapter listAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income_statistics);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(false)
                .navigationBarDarkIcon(true)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(false, 0.2f)
                .init();
        initView();
        initData();
        setListener();
    }

    private void initData() {
        requestData();
    }

    public void initView() {
        homePageModel = new HomePageModelImpl();
        userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        if (userInfoBean != null && userInfoBean.getData() != null) {
            tvName.setText(userInfoBean.getData().getName());
            //创客身份 1是 2否
            maker = userInfoBean.getData().getMaker();
//            红包版运营中心 1是 2否
            hb_operation_type = userInfoBean.getData().getHb_operation_type();
            //是否是机构 1是 2否
            agency = userInfoBean.getData().getAgency();
        }

        titleBar.setRightClickListener(() -> {
            showPictureSelectorDialog();
        });

    }

    public void setListener() {
        listAdapter = new RevenueOverviewListAdapter(dataBeans);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnCallBackListener((type, position) -> {

            List<String> lists = new ArrayList<>();
            lists.add("全部&0");
            for (int i = 0; i < dataBeans.size(); i++) {
                lists.add(dataBeans.get(i).getName() + "&" + dataBeans.get(i).getType());
            }

            Intent intent = new Intent(mActivity, IncomeNewActivity.class);
            intent.putExtra("cycleType", "all");
            intent.putExtra("param", "2018-01-01");
            intent.putExtra("relation_type",Integer.valueOf(dataBeans.get(position).getType()));
            intent.putExtra("coordinate", position+1);
            intent.putExtra("relation_name",dataBeans.get(position).getName());
            intent.putExtra("lists",new Gson().toJson(lists));

            startActivity(intent);
        });


    }

    private void showPictureSelectorDialog() {
        selectPicDialog = new BottomSheetDialog(mActivity, R.style.Dialog);
        selectPicDialog.getWindow().getAttributes().windowAnimations = R.style.commomDialog;
        View view = LayoutInflater.from(mActivity).inflate(R.layout.dialog_bottom_select_pictrue, null);
        // 相册
        TextView album = view.findViewById(R.id.tv_select_pictrue_album);
        // 相机
        TextView camera = view.findViewById(R.id.tv_select_pictrue_camera);
        // 取消
        TextView cancel = view.findViewById(R.id.tv_select_pictrue_cancel);
        album.setText("按日查询");
        camera.setText("按月查询");
        album.setOnClickListener(v -> {
            Intent intent = new Intent(mActivity, IncomeStatisticsQueryActivity.class);
            intent.putExtra("cycleType", "date");
            intent.putExtra("param", DateTimeUtil.getToDayDate2());
            startActivity(intent);
            cancelPictureDialog();
        });
        camera.setOnClickListener(v -> {
            Intent intent = new Intent(mActivity, IncomeStatisticsQueryActivity.class);
            intent.putExtra("cycleType", "month");
            intent.putExtra("param", DateTimeUtil.getMonthDate2());
            startActivity(intent);
            cancelPictureDialog();
        });
        cancel.setOnClickListener(v -> cancelPictureDialog());

        selectPicDialog.setContentView(view);
        selectPicDialog.show();
    }

    private void cancelPictureDialog() {
        if (selectPicDialog != null) {
            selectPicDialog.cancel();
        }
    }

    public void requestData() {

//                @"0":@"全部",
//                @"1":@"分润收益",
//                @"2":@"发展基金",
//                @"3":@"冻结返现",
//                @"5":@"交易奖励(满10000)",
//                @"6":@"达标奖励",
//                @"7":@"管理基金",
//                @"8":@"荣誉董事分红",
//                @"10":@"团队长奖励",
//                @"11":@"级差收益",
//                @"17":@"五台奖励",
//                @"18":@"VIP机具激活奖励",
//                @"19":@"运营中心奖励",
//                @"20":@"交易金排队奖励",
//                @"21":@"奖励金排队奖励",
//                @"22":@"机构提现奖励",
//                @"23":@"红包版运营中心分红",
//                @"24":@"创客分红",
//                @"25":@"红包抽奖奖励",
//                @"26":@"红包收益",
//                @"27":@"红包版运营中心出货奖励",
//                @"28":@"机构出货奖励",
//                @"29":@"红包版运营中心SIM卡奖励",//属于运营中心  hb_op_sim
//                @"30":@"红包版合伙人SIM卡奖励",//属于合伙人  hb_sim

//                share_money  分润收益
//                level_money   级差收益
//                manage_fund  管理基金
//                director_bonuses  董事分红
//                develop_fund  发展基金
//                device_enable  冻结返现
//                order_bonuses_one  交易奖励(满5000)
//                order_bonuses_two  交易奖励(满10000)
//                target_bonuses  达标奖励
//                team_reward 团队长奖励
//                yun_reward  运营中心奖励
//                five_reward 五台奖励
//                vipDeviceReward VIP机具激活奖励
//                oneDeviceReward 交易金排队奖励
//                twoDeviceReward 奖励金排队奖励
//                hb_money 红包抽奖奖励
//                hb_operation_bonuses  红包版运营中心分红
//                maker_bonuses  创客分红
//                hb_yy_shipment_type 红包版运营中心出货奖励
//                hb_jg_shipment_type  机构出货奖励
//                hb_sim  红包版合伙人SIM卡奖励
//                hb_op_sim 红包版运营中心SIM卡奖励
//                agency_withdrawal  机构提现奖励
        ViewLoading.showProgress(mActivity);

        homePageModel.requestGainCount("", "", new DataCallBack<IncomeStatisticsBean>() {
            @Override
            public void onSuccessful(IncomeStatisticsBean bean) {
                String s = new Gson().toJson(bean);
                Log.e("收益接口=",s);

                String share_money = NumberFormatUtils.formatTwoDecimal(bean.getData().getShare_money() / 100);

                String level_money = NumberFormatUtils.formatTwoDecimal(bean.getData().getLevel_money() / 100);

                String manage_fund = NumberFormatUtils.formatTwoDecimal(bean.getData().getManage_fund() / 100);

                String director_bonuses = NumberFormatUtils.formatTwoDecimal(bean.getData().getDirector_bonuses() / 100);

                String develop_fund = NumberFormatUtils.formatTwoDecimal(bean.getData().getDevelop_fund() / 100);

                String device_enable = NumberFormatUtils.formatTwoDecimal(bean.getData().getDevice_enable() / 100);

                String order_bonuses_one = NumberFormatUtils.formatTwoDecimal(bean.getData().getOrder_bonuses_one() / 100);

                String order_bonuses_two = NumberFormatUtils.formatTwoDecimal(bean.getData().getOrder_bonuses_two() / 100);

                String target_bonuses = NumberFormatUtils.formatTwoDecimal(bean.getData().getTarget_bonuses() / 100);

                String team_reward = NumberFormatUtils.formatTwoDecimal(bean.getData().getTeam_reward() / 100);

                String yun_reward = NumberFormatUtils.formatTwoDecimal(bean.getData().getYun_reward() / 100);

                String five_reward = NumberFormatUtils.formatTwoDecimal(bean.getData().getFive_reward() / 100);

                String vip_device_reward = NumberFormatUtils.formatTwoDecimal(bean.getData().getVip_device_reward() / 100);

                String one_device_reward = NumberFormatUtils.formatTwoDecimal(bean.getData().getOne_device_reward() / 100);

                String two_device_reward = NumberFormatUtils.formatTwoDecimal(bean.getData().getTwo_device_reward() / 100);

                String hb_money = NumberFormatUtils.formatTwoDecimal(bean.getData().getHb_money() / 100);

                String hb_operation_bonuses = NumberFormatUtils.formatTwoDecimal(bean.getData().getHb_operation_bonuses() / 100);

                String maker_bonuses = NumberFormatUtils.formatTwoDecimal(bean.getData().getMaker_bonuses() / 100);

                String hb_yy_shipment_type = NumberFormatUtils.formatTwoDecimal(bean.getData().getHb_yy_shipment_type() / 100);

                String hb_jg_shipment_type = NumberFormatUtils.formatTwoDecimal(bean.getData().getHb_jg_shipment_type() / 100);

                String hb_op_sim = NumberFormatUtils.formatTwoDecimal(bean.getData().getHb_op_sim() / 100);

                String hb_sim = NumberFormatUtils.formatTwoDecimal(bean.getData().getHb_sim() / 100);

                String agency_withdrawal = NumberFormatUtils.formatTwoDecimal(bean.getData().getAgency_withdrawal() / 100);

                RevenueListBean.DataBean shareMoneyBean = new RevenueListBean.DataBean();
                shareMoneyBean.setMoney(share_money);
                shareMoneyBean.setName("分润收益");
                shareMoneyBean.setType("1");

                RevenueListBean.DataBean levelMoneyBean = new RevenueListBean.DataBean();
                levelMoneyBean.setMoney(level_money);
                levelMoneyBean.setName("级差收益");
                levelMoneyBean.setType("11");


                RevenueListBean.DataBean manageFundBean = new RevenueListBean.DataBean();
                manageFundBean.setMoney(manage_fund);
                manageFundBean.setName("管理基金");
                manageFundBean.setType("7");

                RevenueListBean.DataBean directorBonusesBean = new RevenueListBean.DataBean();
                directorBonusesBean.setMoney(director_bonuses);
                directorBonusesBean.setName("荣誉董事分红");
                directorBonusesBean.setType("8");

                RevenueListBean.DataBean developFundBean = new RevenueListBean.DataBean();
                developFundBean.setMoney(develop_fund);
                developFundBean.setName("发展基金");
                developFundBean.setType("2");

                RevenueListBean.DataBean deviceEnableBean = new RevenueListBean.DataBean();
                deviceEnableBean.setMoney(device_enable);
                deviceEnableBean.setName("冻结返现");
                deviceEnableBean.setType("3");

                RevenueListBean.DataBean orderBonusesOneBean = new RevenueListBean.DataBean();
                orderBonusesOneBean.setMoney(order_bonuses_one);
                orderBonusesOneBean.setName("交易奖励(满5000)");
                orderBonusesOneBean.setType("4");

                RevenueListBean.DataBean orderBonusesTwoBean = new RevenueListBean.DataBean();
                orderBonusesTwoBean.setMoney(order_bonuses_two);
                orderBonusesTwoBean.setName("交易奖励(满10000)");
                orderBonusesTwoBean.setType("5");

                RevenueListBean.DataBean targetBonusesBean = new RevenueListBean.DataBean();
                targetBonusesBean.setMoney(target_bonuses);
                targetBonusesBean.setName("达标奖励");
                targetBonusesBean.setType("6");

                RevenueListBean.DataBean teamRewardBean = new RevenueListBean.DataBean();
                teamRewardBean.setMoney(team_reward);
                teamRewardBean.setName("团队长奖励");
                teamRewardBean.setType("10");

                RevenueListBean.DataBean yunRewardBean = new RevenueListBean.DataBean();
                yunRewardBean.setMoney(yun_reward);
                yunRewardBean.setName("运营中心奖励");
                yunRewardBean.setType("19");

                RevenueListBean.DataBean fiveRewardBean = new RevenueListBean.DataBean();
                fiveRewardBean.setMoney(five_reward);
                fiveRewardBean.setName("五台奖励");
                fiveRewardBean.setType("17");

                RevenueListBean.DataBean vipDeviceRewardBean = new RevenueListBean.DataBean();
                vipDeviceRewardBean.setMoney(vip_device_reward);
                vipDeviceRewardBean.setName("VIP机具激活奖励");
                vipDeviceRewardBean.setType("18");

                RevenueListBean.DataBean oneDeviceRewardBean = new RevenueListBean.DataBean();
                oneDeviceRewardBean.setMoney(one_device_reward);
                oneDeviceRewardBean.setName("交易金");
                oneDeviceRewardBean.setType("20");


                RevenueListBean.DataBean twoDeviceRewardBean = new RevenueListBean.DataBean();
                twoDeviceRewardBean.setMoney(two_device_reward);
                twoDeviceRewardBean.setName("奖励金");
                twoDeviceRewardBean.setType("21");

                RevenueListBean.DataBean hbMoneyBean = new RevenueListBean.DataBean();
                hbMoneyBean.setMoney(hb_money);
                hbMoneyBean.setName("红包抽奖奖励");
                hbMoneyBean.setType("25");


                RevenueListBean.DataBean hbOperationBonusesBean = new RevenueListBean.DataBean();
                hbOperationBonusesBean.setMoney(hb_operation_bonuses);
                hbOperationBonusesBean.setName("红包版运营中心分红");
                hbOperationBonusesBean.setType("23");


                RevenueListBean.DataBean makerBonusesBean = new RevenueListBean.DataBean();
                makerBonusesBean.setMoney(maker_bonuses);
                makerBonusesBean.setName("创客分红");
                makerBonusesBean.setType("24");


                RevenueListBean.DataBean hbYyShipmentTypeBean = new RevenueListBean.DataBean();
                hbYyShipmentTypeBean.setMoney(hb_yy_shipment_type);
                hbYyShipmentTypeBean.setName("红包版运营中心出货奖励");
                hbYyShipmentTypeBean.setType("27");

                RevenueListBean.DataBean hbJgShipmentTypeBean = new RevenueListBean.DataBean();
                hbJgShipmentTypeBean.setMoney(hb_jg_shipment_type);
                hbJgShipmentTypeBean.setName("机构出货奖励");
                hbJgShipmentTypeBean.setType("28");

                RevenueListBean.DataBean hbOpSimBean = new RevenueListBean.DataBean();
                hbOpSimBean.setMoney(hb_op_sim);
                hbOpSimBean.setName("红包版运营中心SIM卡奖励");
                hbOpSimBean.setType("29");


                RevenueListBean.DataBean hbSimBean = new RevenueListBean.DataBean();
                hbSimBean.setMoney(hb_sim);
                hbSimBean.setName("红包版合伙人SIM卡奖励");
                hbSimBean.setType("30");

                RevenueListBean.DataBean agencyWithdrawalBean = new RevenueListBean.DataBean();
                agencyWithdrawalBean.setMoney(agency_withdrawal);
                agencyWithdrawalBean.setName("机构提现奖励");
                agencyWithdrawalBean.setType("22");



                dataBeans.add(shareMoneyBean);
                dataBeans.add(levelMoneyBean);
                dataBeans.add(manageFundBean);
                dataBeans.add(directorBonusesBean);
                dataBeans.add(developFundBean);
                dataBeans.add(deviceEnableBean);
                dataBeans.add(orderBonusesOneBean);
                dataBeans.add(orderBonusesTwoBean);
                dataBeans.add(targetBonusesBean);
                dataBeans.add(teamRewardBean);
                dataBeans.add(yunRewardBean);
                dataBeans.add(fiveRewardBean);
                dataBeans.add(vipDeviceRewardBean);
                dataBeans.add(oneDeviceRewardBean);
                dataBeans.add(twoDeviceRewardBean);
                dataBeans.add(hbMoneyBean);
                dataBeans.add(hbOperationBonusesBean);
                dataBeans.add(makerBonusesBean);
                dataBeans.add(hbYyShipmentTypeBean);
                dataBeans.add(hbJgShipmentTypeBean);
                dataBeans.add(hbOpSimBean);
                dataBeans.add(hbSimBean);
                dataBeans.add(agencyWithdrawalBean);


                double aDouble = 0;

                for (int i = 0; i < dataBeans.size(); i++) {

                    aDouble =  aDouble+ Double.valueOf(dataBeans.get(i).getMoney());
                }



//            在“收益总览”、“收益”等页面，使用收益类型的地方，
//            判断账号不是机构的话，去除包含“机构”文字的类型；
//            判断账号不是运营中心的话，去除包含“运营中心”文字的类型；
//            判断账号不是创客的话，去除包含“创客”文字的类型；

                if (agency != 1) {
                    //是否是机构 1是 2否
                    for (int i = 0; i < dataBeans.size(); i++) {
                        if (dataBeans.get(i).getName().contains("机构")){
                            dataBeans.remove(dataBeans.get(i));
                        }
                    }
                }
//                if (maker != 1) {
//                    //创客身份 1是 2否
//                    for (int i = 0; i < dataBeans.size(); i++) {
//                        if (dataBeans.get(i).getName().contains("创客")){
//                            dataBeans.remove(dataBeans.get(i));
//                        }
//                    }
//                }
//                if (hb_operation_type != 1) {
////                红包版运营中心 1是 2否
//                    for (int i = 0; i < dataBeans.size(); i++) {
//                        if (dataBeans.get(i).getName().contains("运营中心")){
//                            dataBeans.remove(dataBeans.get(i));
//                        }
//                    }
//                }

                listAdapter.notifyDataSetChanged();

                String total_money = NumberFormatUtils.formatTwoDecimal((aDouble / 100));

                tvOrderFeeCountTotal.setText(total_money);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.tv_look_details})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_look_details:
                List<String>lists = new ArrayList<>();
                lists.add("全部&0");
                for (int i = 0; i < dataBeans.size(); i++) {
                    lists.add(dataBeans.get(i).getName()+"&"+dataBeans.get(i).getName());
                }
                Intent intent = new Intent(mActivity, IncomeNewActivity.class);
                intent.putExtra("cycleType", "all");
                intent.putExtra("param", "2018-01-01");
                intent.putExtra("relation_type", 0);
                intent.putExtra("coordinate", 0);
                intent.putExtra("relation_name", "全部");
                intent.putExtra("lists",new Gson().toJson(lists));
                startActivity(intent);
                break;
        }
    }
}
